
$(document).ready(function () {
    var List = [];
    var url='';
    var FileUrl='';
    var clickFlag=false;
    var form=$('#form');

    var praseFrom=new xhForm($('#form'));

    var iUserId=addressUri.getParam('iUserId');
    var iTTchId=addressUri.getParam('iTTchId');

    $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd',language: 'zh-CN',minView: "month", autoclose:true});
    $('[name$="tTrackTime"]').val(new Date().format('yyyy-MM-dd'));
    $('[name=iUserId]').val(iUserId);
    if(iTTchId!=null){
        $('#sImages').remove();
        $('#sFiles').remove();
        $('#fileInput').parent().remove();
        praseFrom.fill(trackInfo);
    };

    $('#tpSelect').on("change", function (e) {
        var tpid = $(this).val();
        $.ajax({
            url:baseUrl+'Tch/getTp/'+tpid,
            dataType:'json',
            method:'post',
            success:function (data) {
                $('[name=mAppUse]').val(data['mTPContent']);
            },
            error:function (data) {
                console.log(data);
                window.top.swal('','失败','error');
            },
        })
    });

    praseFrom.bindRequired($('#bt_modal_confirm',parent.document));
    //验证
    $("#fileInput").fileinput('destroy');
    fileInput.initFileInput('fileInput',baseUrl+'Tch/ajaxUpload?iUserId='+iUserId);
    var count=0; //记录上传次数
    $("#fileInput").on("fileuploaded", function (event, data, previewId, index) {
        count++;
        List.push({ FileName: data.response.fileName, KeyID: previewId,url:data.response.url })
        if(clickFlag&&(count==data.files.length)){
            clickFlag=false;
            count=0;
            save();
        }
    });

    $("#fileInput").on("filesuccessremove", function (event, data, previewId, index) {
        for (var i = 0; i < List.length; i++) {
            if (List[i].KeyID== data) {
                List.splice(i,1);
            }
        }
    });

    $('#bt_modal_confirm',parent.document).click(function () {
        if ($('[class=file-preview-thumbnails] [id^=preview] ')[0]!=undefined){//有文件且文件未上传
            var isUploaded=true;
            $('[class=file-preview-thumbnails] [id^=preview]').each(function () {
                if(!$(this).hasClass('file-preview-success')&&!$(this).hasClass('file-preview-error')){
                    isUploaded=false;
                }
            });
            if(isUploaded){//文件已上传
                save();
            }else {
                clickFlag=true;
                $("#fileInput").fileinput('upload');
            }
        }else {
            save();
        }

    });

    function save() {
        for (var i = 0; i < List.length; i++) {
            var footerfix= List[i]['url'].split('.');
            footerfix = footerfix[footerfix.length-1];
            footerfix=footerfix.toLowerCase()
            if(footerfix=='jpg'||footerfix=='gif'||footerfix=='png') {  //这里的url只指图片
                url += List[i]['url'] + ',';
            }else {
                FileUrl+=List[i]['url']+',';
            }
        }
        $('#sImages').val(url);
        $('#sFiles').val(FileUrl);
        //var timeWraper=$('#timeWraper',window.top.xhTop.getActiveFrame().frames['operationGradeTrace'].document);
        $.ajax({
            url : baseUrl+'tch/saveTrack',
            data:$("#form").serialize(),
            type:'post',
            success:function (data) {
                if(data=='fail'){
                    alert('内容重复');
                    return false;
                }
            },
            error:function (result) {
                console.log(result);
                window.top.swal('',result['responseJSON']['error'],'error');
            },
            complete :function () {
                window.top.xhTop.getActiveFrame()['track'].location.reload();
                window.top.xhTop.closeModal();
                //清空
                url='';
                FileUrl='';
                List=[];
            }
        });
    }

});